package com.zygvip.demand.http;

import com.zygvip.demand.constant.StatusCode;
import com.zygvip.demand.core.BaseController;
import com.zygvip.demand.core.Result;
import com.zygvip.demand.entity.Expert;
import com.zygvip.demand.entity.TargetUser;
import com.zygvip.demand.service.ExpertService;
import com.zygvip.demand.service.TargetUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Demo class
 *
 * @author dup
 * @Description: dup
 * @date 16:30 2018/4/17
 */
@Controller
@RequestMapping(value = "/home")
public class HomeController extends BaseController {


    @Autowired
    TargetUserService targetUserService;
    @Autowired
    ExpertService expertService;

    @RequestMapping
    public ModelAndView home() {
        return new ModelAndView("/home/index");
    }

    @RequestMapping(value = "/aboutUs")
    public ModelAndView about() {
        return new ModelAndView("/home/about-us");
    }

    @RequestMapping(value = "/more")
    public ModelAndView more(@RequestParam(value = "page_num", defaultValue = "0", required = false) Integer pageNum,
                             @RequestParam(value = "size", defaultValue = "12", required = false) Integer size,
                                         ModelAndView mv) {
        Page<Expert> page = expertService.findExpertPage(pageNum, size);
        mv.setViewName("/home/classification");
        mv.addObject("data", page);
        return mv;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result doSave(TargetUser targetUser) {
        TargetUser saveTargetUser = targetUserService.addTargetUser(targetUser);
        if(saveTargetUser.getId() > 0) {
            return makeResult(StatusCode.OK, saveTargetUser);
        } else {
            return makeResult(StatusCode.ERROR, null);
        }
    }

}
